package com.rlyy.rsj.db;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Properties;

@Component
public class PostgresqlConn {
    private static Properties properties = new Properties();
    private static final Logger logger = LoggerFactory.getLogger(PostgresqlConn.class);

    private static DataSource dataSource;

    private static String url;

    /**
     * 数据库url
     * @param url url
     */
    @Value("${spring.datasource.url}")
    public void setUrl(String url) {
        PostgresqlConn.url = url;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        PostgresqlConn.dataSource = dataSource;
    }



    public static Connection getConnection() {
        Connection connection;
        try {
            connection = DataSourceUtils.getConnection(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("pg数据库连接失败！");
        }
        return connection;
    }


    public static Connection getConnectionMonitor() {
        return getConnection();
    }

    public static String getConnectionUriMonitor() {
        return url;
    }

}
